<template>
  <a-card title="科目" :bordered="false">
    <!-- <div slot="extra" class="drawer-bootom-button">
      <a-dropdown :trigger="['click']" placement="topCenter">
        <a-menu slot="overlay">
          <a-menu-item key="5" @click="expandAll">展开所有</a-menu-item>
          <a-menu-item key="6" @click="closeAll">合并所有</a-menu-item>
        </a-menu>
        <a-button>
          树操作
          <a-icon type="up"/>
        </a-button>
      </a-dropdown>
    </div> -->
    <div style="background: #fff">
      <a-col :md="10" :sm="24">
        <template>
          <a-dropdown :trigger="[this.dropTrigger]" @visibleChange="dropStatus">
            <span style="user-select: none">
              <!-- @rightClick="rightHandle" -->
              <a-tree v-if="departTree.length > 0" 
                v-model="checkedKeys" 
                :show-line="true" 
                @select="onSelect"
                @check="onCheck" 
                :selectedKeys="selectedKeys" 
                :checkedKeys="checkedKeys" 
                :treeData="departTree"
                :checkStrictly="checkStrictly" 
                :expandedKeys="iExpandedKeys" 
                :autoExpandParent="autoExpandParent"
                @expand="onExpand">
                <a-icon slot="switcherIcon" type="down" />
              </a-tree>
            </span>
            <!--新增右键点击事件,和增加添加和删除功能-->
            <a-menu slot="overlay">
              <a-menu-item @click="handleAdd()" key="1">添加节点</a-menu-item>
              <!--              <a-menu-item @click="handleEdit" key="2">编辑节点</a-menu-item>-->
              <a-menu-item @click="handleDelete" key="3">删除节点</a-menu-item>
              <!--              <a-menu-item @click="closeDrop" key="4">取消操作</a-menu-item>-->
            </a-menu>
          </a-dropdown>
        </template>
      </a-col>
    </div>
    <!-- <addTreeNode ref="addTreeNode" @ok="loadTree" :templateId="templateId" :rowData="model"></addTreeNode> -->
  </a-card>
</template>

<script>
import { getAction, deleteAction } from '@/api/manage'
import { JeecgListMixin } from '@/mixins/JeecgListMixin'
// import addTreeNode from "./addTreeNode";

export default {
  name: 'LeftTree',
  props: ['templateId', 'model'],
  mixins: [JeecgListMixin],
  components: {},
  data() {
    return {
      rightClickSelectedKey: '',
      rightClickSelectedNode: '',
      dropTrigger: '',
      iExpandedKeys: [],
      loading: false,
      autoExpandParent: true,
      currFlowId: '',
      currFlowName: '',
      departTree: [],
      hiding: true,
      checkedKeys: [],
      selectedKeys: [],
      currSelected: {},
      allTreeKeys: [],
      checkStrictly: true,
      url: '/sjkmfx/sjSubjectsAnalyse/queryAgingSubject',
      orgCategoryDisabled: false,
    }
  },
  computed: {},
  created() {
    this.loadTree()
  },
  watch: {
    $route: {
      handler: function (to) {
        if (Object.keys(to.params).length && to.params.from == 'sjxmss') {
          this.selectedKeys = ['1122']
          this.$emit('selected', this.selectedKeys[0])
        }
      },
      immediate: true,
    },
  },
  methods: {
    loadData() {
      this.refresh()
    },
    handleAdd(root) {
      if (root === 'root') {
        this.$refs.addTreeNode.add(null)
      } else {
        this.$refs.addTreeNode.add(this.rightClickSelectedNode)
      }
    },
    // handleEdit() {
    //   this.$refs.addTreeNode.edit(this.rightClickSelectedNode)
    // },
    handleDelete() {
      this.$confirm({
        title: '确认删除',
        content: '确定要删除此部门以及子节点数据吗?',
        onOk: () => {
          deleteAction('/sjxmzygl/sjProjectTemplateTreeFa/updDelFlagById', {
            id: this.rightClickSelectedNode.jid,
          }).then((res) => {
            if (res.success) {
              this.$message.success('删除成功!')
              this.loadTree()
            }
          })
        },
      })
    },
    // 右键点击下拉框改变事件
    dropStatus(visible) {
      if (visible == false) {
        this.dropTrigger = ''
      }
    },
    // 右键下拉关闭下拉框
    closeDrop() {
      this.dropTrigger = ''
    },
    rightHandle(node) {
      this.dropTrigger = 'contextmenu'
      console.log(node.node.eventKey)
      this.rightClickSelectedKey = node.node.eventKey
      this.rightClickSelectedNode = node.node.dataRef
    },
    loadTree() {
      var that = this
      that.departTree = []
      getAction(this.url).then((res) => {
        if (res.success) {
          //部门全选后，再添加部门，选中数量增多
          this.allTreeKeys = []
          this.departTree = this.getTree(res.result, '0')
          this.loading = false
        }
      })
    },
    getTree(treeData, pid) {
      let treeArr = []
      for (let i = 0; i < treeData.length; i++) {
        let node = treeData[i]
        if (node.pcode == pid) {
          let newNode = {
            title: node.subjectName,
            key: node.subjectCode,
            children: this.getTree(treeData, node.subjectCode),
          }
          treeArr.push(newNode)
        }
      }
      return treeArr
    },
    setThisExpandedKeys(node) {
      if (node.children && node.children.length > 0) {
        this.iExpandedKeys.push(node.key)
        for (let a = 0; a < node.children.length; a++) {
          this.setThisExpandedKeys(node.children[a])
        }
      }
    },
    refresh() {
      this.loading = true
      this.loadTree()
    },
    onExpand(expandedKeys) {
      console.log('onExpand', expandedKeys)
      this.iExpandedKeys = expandedKeys
      this.autoExpandParent = false
    },
    nodeModalOk() {
      this.loadTree()
    },
    nodeModalClose() { },
    onCheck(checkedKeys, info) {
      console.log('onCheck', checkedKeys, info)
      this.hiding = false
      //---- author:os_chengtgen -- date:20190827 --  for:切换父子勾选模式 =======------
      if (this.checkStrictly) {
        this.checkedKeys = checkedKeys.checked
      } else {
        this.checkedKeys = checkedKeys
      }
      //---- author:os_chengtgen -- date:20190827 --  for:切换父子勾选模式 =======------
    },
    onSelect(selectedKeys, e) {
      console.log('selected', selectedKeys, e)
      this.hiding = false
      let record = {}
      if (e) {
        record = e.node.dataRef
      } else {
        record.key = selectedKeys
      }

      this.currSelected = Object.assign({}, record)
      this.selectedKeys = [record.key]
      this.$emit('selected', selectedKeys[0])
    },
    getCurrSelectedTitle() {
      return !this.currSelected.title ? '' : this.currSelected.title
    },
    onClearSelected() {
      this.hiding = true
      this.checkedKeys = []
      this.currSelected = {}
      this.selectedKeys = []
      this.$refs.departAuth.departId = ''
    },
    //---- author:os_chengtgen -- date:20190827 --  for:切换父子勾选模式 =======------
    expandAll() {
      this.iExpandedKeys = this.allTreeKeys
    },
    closeAll() {
      this.iExpandedKeys = []
    },

    //---- author:os_chengtgen -- date:20190827 --  for:切换父子勾选模式 =======------
  },
}
</script>

<style lang="less" scoped>
.ant-card {
  height: 100%;

  /deep/ .ant-card-head {
    padding: 0 15px;

    /deep/ .ant-card-head-title,
    /deep/ .ant-card-extra {
      padding: 10px 0;
    }
  }

  /deep/ .ant-card-body {
    padding: 10px;
    height: calc(100vh - 250px);
    overflow: auto;
  }
}
</style>